package 二叉树相关问题.BST;

public class 二叉搜索树的最近公共祖先 {

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
           if (root==null)
               return root;
           if ((p.val<=root.val && root.val<=q.val) || (p.val>=root.val && root.val>=q.val) || p.val==root.val || q.val==root.val)
               return root;
           else if (p.val>root.val && q.val>root.val)
               return lowestCommonAncestor(root.right,p,q);
           else
               return lowestCommonAncestor(root.left,p,q);
    }
}
